//
//  GHPhotoBrowserController.swift
//  Browser
//
//  Created by 刘光华 on 16/4/28.
//  Copyright © 2016年 liuguanghua. All rights reserved.
//

import UIKit
import Masonry

class GHPhotoBrowserController: UIViewController {

    let GHPhotoBrowserCellId = "GHPhotoBrowserCellId"
    
    var shops: [GHShop]?
    var indexPath : NSIndexPath?
    
    // MARK:- 懒加载
    lazy var saveButton: UIButton = {
       let saveButton = UIButton(title: "保存", backgroundColor: UIColor.clearColor(), font: UIFont.systemFontOfSize(14))
        saveButton.addTarget(self, action: "saveButtonClick", forControlEvents: .TouchUpInside)
        return saveButton
    }()
    lazy var closeButton: UIButton = {
        let closeButton = UIButton(title: "关闭", backgroundColor: UIColor.clearColor(), font: UIFont.systemFontOfSize(14))
        closeButton.addTarget(self, action: "closeButtonClick", forControlEvents: .TouchUpInside)
        
        return closeButton
    }()
    lazy var collectionView: UICollectionView = {
       let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: GHPhotoBrowserCollectionViewLayout())
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.pagingEnabled = true
        collectionView .registerClass(GHPhotoBrowserCell.self, forCellWithReuseIdentifier: self.GHPhotoBrowserCellId)
        return collectionView
    }()
    
    // MARK:- 系统回调
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 设置UI
        setupUI()
        
    }
    
    override func viewDidLayoutSubviews() {
        
        super.viewDidLayoutSubviews()
        // 设置起始位置
        collectionView.scrollToItemAtIndexPath(indexPath!, atScrollPosition: .Left, animated: false)
        
    }
    
}


// MARK:- 设置UI
extension GHPhotoBrowserController {
    
    // 设置UI
    func setupUI() {
        
        view.addSubview(collectionView)
        view.addSubview(saveButton)
        view.addSubview(closeButton)
        
        // 设置约束
        setupConstraints()
        
    }
    
    /// 设置约束
    func setupConstraints() {
        
        closeButton.mas_makeConstraints { (make: MASConstraintMaker!) -> Void in
            make.left.offset()(30)
            make.bottom.offset()(-30)
            make.width.equalTo()(90)
            make.height.equalTo()(32)
        }
        saveButton.mas_makeConstraints { (make: MASConstraintMaker!) -> Void in
            make.right.bottom().offset()(-30)
            make.width.equalTo()(90)
            make.height.equalTo()(32)
        }
        collectionView.mas_makeConstraints { (make: MASConstraintMaker!) -> Void in
            make.edges.equalTo()(self.view)
        }
    }
}

// MARK:- GHDismissDelegate
extension GHPhotoBrowserController: GHDismissDelegate {
    
    /// 获取当前显示的下标
    func currentIndexPath() -> NSIndexPath {
        
        let cell = collectionView.visibleCells().first!
        
        let indexPath = collectionView.indexPathForCell(cell)
        
        return indexPath!
        
    }
    
    /// 获取imageView
    func imageView() -> UIImageView {
        
        let cell = collectionView.visibleCells().first as? GHPhotoBrowserCell
        
        let image = cell?.imageView.image
        
        let imageView = UIImageView()
        
        imageView.contentMode = .ScaleAspectFill
        imageView.clipsToBounds = true
        
        imageView.image = image
        
        return imageView
        
    }
    
    /// 获取当前imageView的尺寸
    func imageViewHomePosition(indexPath: NSIndexPath) -> CGRect {
        
        let cell = collectionView.cellForItemAtIndexPath(indexPath) as! GHPhotoBrowserCell
        
        let image = cell.imageView.image!
        
        return calculateImageViewHeight(image)
        
    }
    
}

// MARK:- UICollectionViewDataSource

extension GHPhotoBrowserController: UICollectionViewDataSource, UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return shops?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(GHPhotoBrowserCellId, forIndexPath: indexPath) as! GHPhotoBrowserCell
        
        cell.shop = shops?[indexPath.item]
        
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        closeButtonClick()
    }
    
}

// MARK:- 事件响应
extension GHPhotoBrowserController {
    
    // 如果声明为私有的就必须加@objc
    @objc private func closeButtonClick() {
        
        dismissViewControllerAnimated(true, completion: nil)
        
    }
    
    @objc private func saveButtonClick() {
        
        let cell = collectionView.visibleCells().first as! GHPhotoBrowserCell
        
        let image = cell.imageView.image
        
        UIImageWriteToSavedPhotosAlbum(image!, nil, nil, nil)
        
    }
    
    
    
}












